package cn.dgut.training.service.impl;

import cn.dgut.training.controller.UserService;
import cn.dgut.training.dto.goods.CommentDTO;
import cn.dgut.training.entity.CommentCount;
import cn.dgut.training.entity.CommentDisplay;
import cn.dgut.training.entity.goods.Comment;
import cn.dgut.training.entity.user.User;
import cn.dgut.training.mapper.CommentMapper;
import cn.dgut.training.service.ICommentService;
import cn.dgut.training.vo.BaseVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Resource
    private CommentMapper commentMapper;


    @Autowired
    private UserService userService;

    public Map<String,Object> listCommentByPage(CommentDTO commentDTO, int showType){
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>();
        if (commentDTO.getValueId() != -1){
            queryWrapper.eq("value_id", commentDTO.getValueId());
        }
        queryWrapper.eq("comment_type", commentDTO.getCommentType());
        if (showType == 1){
            queryWrapper.eq("has_picture", true);
        }
        IPage<Comment> page=new Page<Comment>();
        BeanUtils.copyProperties(commentDTO,page);
        IPage<Comment> res=commentMapper.selectPage(page,queryWrapper);
        Map<String,Object> data=new HashMap<>();
        data.put("current", res.getCurrent());
        data.put("pages", res.getPages());
        data.put("size", res.getSize());
        data.put("total", res.getTotal());
        List l = res.getRecords();
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        for (Object o : l) {
            Comment comment = (Comment)o;
            Map<String, Object> c = new HashMap<>();
            c.put("id", comment.getId());
            c.put("addTime", sdf.format((Date)comment.getAddTime()));
            c.put("content", comment.getContent());
            c.put("star", comment.getStar());
            User user = userService.iGetUserById(Long.valueOf(comment.getUserId()));
            c.put("nickname", user == null ? "" : user.getNickname());
            c.put("avatar", user == null ? "" : user.getAvatar());
            String[] PicUrls=comment.getPicUrls().split(",");
            c.put("picList", PicUrls);
            mapList.add(c);
        }


        data.put("list", mapList);
        return data;
    }


    public Map<String,Object> listAllGoodsCommentByPage(CommentDTO commentDTO){
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>();
        if (commentDTO.getValueId() != -1){
            queryWrapper.eq("value_id", commentDTO.getValueId());
        }
        queryWrapper.eq("comment_type", commentDTO.getCommentType());
        IPage<Comment> page=new Page<Comment>();
        BeanUtils.copyProperties(commentDTO,page);
        IPage<Comment> res=commentMapper.selectPage(page,queryWrapper);
        Map<String,Object> data=new HashMap<>();
        data.put("current", res.getCurrent());
        data.put("pages", res.getPages());
        data.put("size", res.getSize());
        data.put("total", res.getTotal());
        List l = res.getRecords();
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        for (Object o : l) {
            Comment comment = (Comment)o;
            Map<String, Object> c = new HashMap<>();
            c.put("id", comment.getId());
            c.put("addTime", sdf.format((Date)comment.getAddTime()));
            c.put("userId", comment.getUserId());
            c.put("valueId", comment.getValueId());
            c.put("star", comment.getStar());
            c.put("content", comment.getContent());
            c.put("hasPicture", comment.getHasPicture());
            User user = userService.iGetUserById(Long.valueOf(comment.getUserId()));
            c.put("nickname", user == null ? "" : user.getNickname());
            c.put("avatar", user == null ? "" : user.getAvatar());
            if(comment.getHasPicture() == true){
                String[] PicUrls=comment.getPicUrls().split(",");
                c.put("picUrls", PicUrls);
            }else{
                c.put("picUrls", "");
            }
            mapList.add(c);
        }


        data.put("list", mapList);
        return data;
    }



    public CommentCount countComment(long valueId, long type){
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>();
        queryWrapper.eq("value_id", valueId);
        queryWrapper.eq("comment_type", type);
        int allCount=commentMapper.selectCount(queryWrapper);
        queryWrapper.eq("has_picture", true);
        int hasPicCount=commentMapper.selectCount(queryWrapper);
        CommentCount commentCount = new CommentCount(allCount,hasPicCount);
        return commentCount;
    }

    public BaseVO addOrderGoodsComment(CommentDTO commentDTO){
        commentDTO.setCommentType(3);

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);

        this.save(comment);

        BaseVO<Comment> commentBaseVO = new BaseVO<>();
        List<Comment> commentList=new ArrayList<>();
        commentList.add(comment);
        commentBaseVO.setList(commentList);

        return commentBaseVO;
    }


    public Map<String, Object> getGoodsComment(Long goodsId){

        List<Comment> comments = commentMapper.selectList(new LambdaQueryWrapper<Comment>()
            .eq(Comment::getValueId,goodsId).last("limit 0,2"));

        List<Map<String, Object>> commentsVo = new ArrayList<>(comments.size());

        long commentCount = commentMapper.selectCount(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getValueId,goodsId));

        for (Comment comment : comments) {
            Map<String, Object> c = new HashMap<>();
            c.put("id", comment.getId());
            c.put("addTime", comment.getAddTime());
            c.put("content", comment.getContent());
//            User user = userService.findById(comment.getUserId());
//            c.put("nickname", user == null ? "" : user.getNickname());
//            c.put("avatar", user == null ? "" : user.getAvatar());
            String[] PicUrls=comment.getPicUrls().split(",");
            c.put("picList", PicUrls);
            commentsVo.add(c);
        }
        Map<String, Object> commentList = new HashMap<>();
        commentList.put("allCount", commentCount);
        commentList.put("data", commentsVo);
        return commentList;
    }

}
